// 创建APP
const express = require('express');
const app = express();
var expressWs = require('express-ws');
expressWs(app);

// 配置cookie
const cookieParser = require('cookie-parser');
app.use(cookieParser());

// 解析form-data数据
const bodyParser = require('body-parser');
// 数据JSON类型
app.use(bodyParser.json());
// 解析post请求数据
app.use(bodyParser.urlencoded({extended: false}));

// 引入全局变量
require('./common/global');

// 允许访问图片目录
// app.get('/uploads/*', function (req, res) {
//     res.sendFile( __dirname + "/" + req.url );
// });

// 生成公钥、私钥，用于cookie加密
const RSA = require('./common/RSAClass');
new RSA().creatToken();


// 设置允许跨域
app.all('*', function(req, res, next) {
    if (/(10\.60\.*)|(192\.168\.*)/g.test(req.headers.referer)) {
        res.header("Access-Control-Allow-Origin", 'http://10.60.6.109:5173'); //需要显示设置来源
        res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        res.header("Access-Control-Allow-Methods", "POST");
        res.header("Access-Control-Allow-Credentials", true); //带cookies
        res.header("Content-Type", "application/json;charset=utf-8");
        next();
    }
});


// 路由
app.use('/', require('./router/api-ws'))

// 监听端口
app.listen('3333',()=>{
    console.log('app server success! path: 3333')
});


module.exports = app;